﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_CommentDAL : IDcms_CommentDAL
    {
        public Dcms_CommentModel AddModel(Dcms_CommentModel model)
        {
            string sQLString = "insert into Dcms_Comment(Comment_Title,Comment_Content,Comment_UserID,Comment_IP,Comment_AddTime,Comment_Revert,Comment_RevertTime,Comment_RevertUserID,Comment_RevertIP,Comment_ModelFlag,Comment_ModelKeyID,Comment_FlagID) values(@Comment_Title,@Comment_Content,@Comment_UserID,@Comment_IP,@Comment_AddTime,@Comment_Revert,@Comment_RevertTime,@Comment_RevertUserID,@Comment_RevertIP,@Comment_ModelFlag,@Comment_ModelKeyID,@Comment_FlagID) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@Comment_Title", SqlDbType.VarChar), new SqlParameter("@Comment_Content", SqlDbType.Text), new SqlParameter("@Comment_UserID", SqlDbType.VarChar), new SqlParameter("@Comment_IP", SqlDbType.VarChar), new SqlParameter("@Comment_AddTime", SqlDbType.DateTime), new SqlParameter("@Comment_Revert", SqlDbType.Text), new SqlParameter("@Comment_RevertTime", SqlDbType.DateTime), new SqlParameter("@Comment_RevertUserID", SqlDbType.VarChar), new SqlParameter("@Comment_RevertIP", SqlDbType.VarChar), new SqlParameter("@Comment_ModelFlag", SqlDbType.VarChar), new SqlParameter("@Comment_ModelKeyID", SqlDbType.SmallInt), new SqlParameter("@Comment_FlagID", SqlDbType.Int) };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.Comment_Title;
            cmdParms[2].Value = model.Comment_Content;
            cmdParms[3].Value = model.Comment_UserID;
            cmdParms[4].Value = model.Comment_IP;
            cmdParms[5].Value = model.Comment_AddTime;
            cmdParms[6].Value = model.Comment_Revert;
            cmdParms[7].Value = model.Comment_RevertTime;
            cmdParms[8].Value = model.Comment_RevertUserID;
            cmdParms[9].Value = model.Comment_RevertIP;
            cmdParms[10].Value = model.Comment_ModelFlag;
            cmdParms[11].Value = model.Comment_ModelKeyID;
            cmdParms[12].Value = model.Comment_FlagID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.Comment_ID = (int) cmdParms[0].Value;
            return model;
        }

        public IList<Dcms_CommentModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_CommentModel> list = new List<Dcms_CommentModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_CommentModel item = new Dcms_CommentModel();
                if (ds.Tables[0].Rows[i]["Comment_ID"].ToString() != "") item.Comment_ID = int.Parse(ds.Tables[0].Rows[i]["Comment_ID"].ToString());
                item.Comment_Title = ds.Tables[0].Rows[i]["Comment_Title"].ToString();
                item.Comment_Content = ds.Tables[0].Rows[i]["Comment_Content"].ToString();
                item.Comment_UserID = ds.Tables[0].Rows[i]["Comment_UserID"].ToString();
                item.Comment_IP = ds.Tables[0].Rows[i]["Comment_IP"].ToString();
                if (ds.Tables[0].Rows[i]["Comment_AddTime"].ToString() != "") item.Comment_AddTime = DateTime.Parse(ds.Tables[0].Rows[i]["Comment_AddTime"].ToString());
                item.Comment_Revert = ds.Tables[0].Rows[i]["Comment_Revert"].ToString();
                if (ds.Tables[0].Rows[i]["Comment_RevertTime"].ToString() != "") item.Comment_RevertTime = DateTime.Parse(ds.Tables[0].Rows[i]["Comment_RevertTime"].ToString());
                item.Comment_RevertUserID = ds.Tables[0].Rows[i]["Comment_RevertUserID"].ToString();
                item.Comment_RevertIP = ds.Tables[0].Rows[i]["Comment_RevertIP"].ToString();
                item.Comment_ModelFlag = ds.Tables[0].Rows[i]["Comment_ModelFlag"].ToString();
                if (ds.Tables[0].Rows[i]["Comment_ModelKeyID"].ToString() != "") item.Comment_ModelKeyID = int.Parse(ds.Tables[0].Rows[i]["Comment_ModelKeyID"].ToString());
                if (ds.Tables[0].Rows[i]["Comment_FlagID"].ToString() != "") item.Comment_FlagID = int.Parse(ds.Tables[0].Rows[i]["Comment_FlagID"].ToString());
                list.Add(item);
            }
            return list;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Comment where Comment_ID=@Comment_ID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Comment_ID", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return SqlHelper.GetNoteCount(SqlStr).ToString();
        }

        public IList<Dcms_CommentModel> GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return this.DataSetToList(SqlHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize));
        }

        public Dcms_CommentModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_Comment where Comment_ID=@Comment_ID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Comment_ID", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_CommentModel model = new Dcms_CommentModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_CommentModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public IList<Dcms_CommentModel> GetModelList()
        {
            string sql = "select * from Dcms_Comment";
            return this.DataSetToList(SqlHelper.CreateDataSet(sql));
        }

        public Dcms_CommentModel Update(Dcms_CommentModel model)
        {
            string sQLString = "update Dcms_Comment set Comment_Title=@Comment_Title,Comment_Content=@Comment_Content,Comment_UserID=@Comment_UserID,Comment_IP=@Comment_IP,Comment_AddTime=@Comment_AddTime,Comment_Revert=@Comment_Revert,Comment_RevertTime=@Comment_RevertTime,Comment_RevertUserID=@Comment_RevertUserID,Comment_RevertIP=@Comment_RevertIP,Comment_ModelFlag=@Comment_ModelFlag,Comment_ModelKeyID=@Comment_ModelKeyID,Comment_FlagID=@Comment_FlagID where Comment_ID=@Comment_ID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Comment_Title", SqlDbType.VarChar), new SqlParameter("@Comment_Content", SqlDbType.Text), new SqlParameter("@Comment_UserID", SqlDbType.VarChar), new SqlParameter("@Comment_IP", SqlDbType.VarChar), new SqlParameter("@Comment_AddTime", SqlDbType.DateTime), new SqlParameter("@Comment_Revert", SqlDbType.Text), new SqlParameter("@Comment_RevertTime", SqlDbType.DateTime), new SqlParameter("@Comment_RevertUserID", SqlDbType.VarChar), new SqlParameter("@Comment_RevertIP", SqlDbType.VarChar), new SqlParameter("@Comment_ModelFlag", SqlDbType.VarChar), new SqlParameter("@Comment_ModelKeyID", SqlDbType.SmallInt), new SqlParameter("@Comment_FlagID", SqlDbType.Int), new SqlParameter("@Comment_ID", SqlDbType.Int) };
            cmdParms[0].Value = model.Comment_Title;
            cmdParms[1].Value = model.Comment_Content;
            cmdParms[2].Value = model.Comment_UserID;
            cmdParms[3].Value = model.Comment_IP;
            cmdParms[4].Value = model.Comment_AddTime;
            cmdParms[5].Value = model.Comment_Revert;
            cmdParms[6].Value = model.Comment_RevertTime;
            cmdParms[7].Value = model.Comment_RevertUserID;
            cmdParms[8].Value = model.Comment_RevertIP;
            cmdParms[9].Value = model.Comment_ModelFlag;
            cmdParms[10].Value = model.Comment_ModelKeyID;
            cmdParms[11].Value = model.Comment_FlagID;
            cmdParms[12].Value = model.Comment_ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }
    }
}

